package com.springboot.demo.mapper;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.springboot.demo.entity.*;
import io.swagger.annotations.ApiModelProperty;
import org.apache.ibatis.annotations.*;
import org.hibernate.validator.constraints.Length;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * @author wubc683
 * @desc
 * @date 2021-11-22 15:16
 */
public interface PanelMapper {
    @Select("select id,name,type,createDate,isDefault from web_panel where id in (select panelId from web_user_operate_panel where userId = " +
            "#{uid})")
    List<Panel> getAllOperatePanels(Long uid);

    @Select("select name from web_panel where id in (select panelId from web_user_operate_panel where userId = #{uid})")
    List<String> getAllOperatePanelNames(Long uid);

    @Insert("insert into web_panel(name,type,createDate,isDefault) values(#{name},#{type},#{createDate},#{isDefault})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void savePanel(Panel panel);

    @Insert("insert into web_user_operate_panel(userId,panelId) values(#{uid},#{panelId})")
    void saveUserOperatePanel(Long uid, Long panelId);

    @Select("select id from web_panel where name = #{name} and id in (select panelId from web_user_operate_panel where " +
            "userId = #{uid})")
    Long getOperatePanelIdByUidAndName(Long uid, String name);

    @Select("select userId from web_user_operate_panel where userId = #{uid} and panelId = #{panelId}")
    Long getOperatePanelUserIdByUidAndPanelId(Long uid, Long panelId);

    @Update("update web_panel set name = #{name},isDefault = #{isDefault} where id = #{id}")
    void updateOperatePanel(Panel panel);

    @Delete("delete from web_user_operate_panel where panelId = #{pid}")
    void deleteOperatePanelByPanelId(Long pid);

    @Delete("delete from web_panel where id = #{pid}")
    void deletePanelById(Long pid);

    @Select("select id,name,type,createDate,isDefault from web_panel where id in " +
            "(select panelId from web_scene_panel where sceneId = #{sceneId})")
    List<Panel> getAllScenePanels(Long sceneId);

    @Select("select name from web_panel where id in(select panelId from web_scene_panel where sceneId = #{sceneId})")
    List<String> getAllScenePanelNames(Long sceneId);

    @Insert("insert into web_scene_panel(sceneId,panelId) values(#{sceneId},#{panelId})")
    void saveScenePanel(Long sceneId, Long panelId);

    @Select("select name from web_panel where id in (select panelId from web_user_operate_panel where userId = #{uid}" +
            " and panelId <> #{panelId}) ")
    List<String> getAllOperatePanelNamesExceptPid(Long uid, Long panelId);

    @Update("update web_panel set name = #{name} where id = #{id}")
    void updatePanel(Panel panel);

    @Select("select name from web_panel where id in (select panelId from web_scene_panel where sceneId = #{sceneId} " +
            "and" + " panelId <> #{panelId})")
    List<String> getAllScenePanelNamesExceptPid(Long sceneId, Long panelId);

    @Select("select sceneId from web_scene_panel where panelId = #{panelId}")
    Long getSceneIdByPanelId(Long panelId);

    @Delete("delete from web_scene_panel where panelId = #{pid}")
    void deleteScenePanelByPanelId(Long pid);

    @Select("select m.id,disperseType,longitude,latitude,color,scale from web_map m " +
            " left join web_panel_map pm on m.id = pm.mapId where pm.panelId = #{pid}")
    @Results({// @Results：封装映射关系的父注解
            @Result(column = "id", property = "id"), //@Result：封装映射关系的子注解
            @Result(column = "disperseType", property = "disperseType"),
            @Result(column = "longitude", property = "longitude"),
            @Result(column = "latitude", property = "latitude"),
            @Result(column = "color", property = "color"),
            @Result(column = "scale", property = "scale"),
            //column 属性：查询出的表中字段名称
            //property 属性：实体对象中的属性名称
            @Result(property = "position",
                    javaType = ComponentPosition.class,
                    column = "id",
                    many = @Many(select = "com.springboot.demo.mapper.PanelMapper.getMapPositionByMapId")
            )//many 属性：一对多查询固定属性
    })
    MapData getPanelMapByPanelId(Long pid);

    @Select("select  leftPos,rightPos,topPos,downPos from web_panel_map where mapId = #{mapId}")
    ComponentPosition getMapPositionByMapId(Long mapId);

    @Select("select tableId ,#{pid} as panelId from web_panel_table where panelId = #{pid}")
    @Results({
            @Result(column = "tableId", property = "tableId"),
            @Result(property = "position",
                    javaType = ComponentPosition.class,
                    column = "{tableId=tableId,panelId=panelId}",
                    many = @Many(select = "com.springboot.demo.mapper.PanelMapper.getPanelTablePositionByTableId")
            )
    })
    List<TableData> getPanelTablesByPanelId(Long pid);

    @Select("select leftPos,rightPos,topPos,downPos from web_panel_table where panelId = #{panelId} and tableId = " +
            "#{tableId}")
    ComponentPosition getPanelTablePositionByTableId(Long panelId,Long tableId);

    @Select("select sceneGraphId ,leftPos,rightPos,topPos,downPos from web_panel_scene_graph where panelId = #{pid}")
    SceneGraph getPanelSceneGraphByPanelId(Long pid);

    @Insert("insert into web_panel_table(panelId,tableId,leftPos,rightPos,topPos,downPos) values(#{panelId}," +
            "#{componentId},#{position.leftPos},#{position.rightPos},#{position.topPos},#{position.downPos})")
    void savePanelTable(PanelComponent component);

    @Select("select tableId from web_panel_table where panelId = #{panelId}")
    List<Long> getPanelTableIdsByPanelId(Long panelId);

    @Insert("insert into web_panel_map(panelId,mapId,leftPos,rightPos,topPos,downPos) values(#{panelId}," +
            "#{componentId},#{position.leftPos},#{position.rightPos},#{position.topPos},#{position.downPos})")
    void savePanelMap(PanelComponent component);

    @Select("select mapId from web_panel_map where panelId = #{panelId}")
    Long getPanelMapIdByPanelId(Long panelId);

    @Select("select ml.mapId,ml.leftPos,ml.rightPos,ml.topPos,ml.downPos from web_map_link ml left join web_panel_map" +
            " pm on ml" + ".mapId = pm.mapId where pm.panelId = #{panelId}")
    Long getPanelMapLinkDataByPanelId(Long panelId);

    @Update("update web_panel_table set leftPos = #{position.leftPos},rightPos=#{position.rightPos},topPos=#{position.topPos}," +
            "downPos=#{position.downPos}" +
            " where panelId = #{panelId} and tableId = #{componentId}")
    void updatePanelTable(PanelComponent component);

    @Update("update web_panel_map set leftPos = #{position.leftPos},rightPos=#{position.rightPos},topPos=#{position.topPos}," +
            "downPos=#{position.downPos} where panelId = #{panelId} ")
    void updatePanelMap(PanelComponent component);

    @Delete("delete from web_panel_table where panelId =#{panelId} and tableId = #{componentId}")
    void deletePanelTable(PanelComponent component);

    @Delete("delete from web_panel_map where mapId = #{mapId}")
    void deletePanelMap(Long mapId);

    @Select("select type from web_panel where id = #{pid}")
    Integer getPanelTypeById(Long pid);

    @Select("select id,name,type,createDate,isDefault from web_panel where id = #{pid}")
    Panel getPanelById(Long pid);

    @Delete("delete from web_panel_map where panelId = #{pid}")
    void deletePanelMapByPanelId(Long pid);

    @Delete("delete from web_panel_table where panelId = #{pid}")
    void deletePanelTableByPanelId(Long pid);

    @Select("<script> select panelId from web_scene_panel where sceneId in <foreach item='item' " +
            " collection='sceneIds' open = '(' close = ')' separator = ','> #{item} </foreach> </script>")
    List<Long> getPanelIdsBySceneId(List<Long> sceneIds);

    @Delete("<script> delete from web_panel where id in <foreach item='item' " +
            " collection='panelIds' open = '(' close = ')' separator = ','> #{item} </foreach> </script>")
    void deletePanelsByPanelIds(List<Long> panelIds);

    @Delete("<script> delete from web_panel_table where panelId in <foreach item='item' " +
            " collection='panelIds' open = '(' close = ')' separator = ','> #{item} </foreach> </script>")
    void deletePanelTablesByPanelIds(List<Long> panelIds);

    @Delete("<script> delete from web_panel_map where panelId in <foreach item='item' " +
            " collection='panelIds' open = '(' close = ')' separator = ','> #{item} </foreach> </script>")
    void deletePanelMapsByPanelIds(List<Long> panelIds);

    @Delete("<script> delete from web_scene_panel where sceneId in <foreach item='item' " +
            " collection='sceneIds' open = '(' close = ')' separator = ','> #{item} </foreach> </script>")
    void deleteScenePanelsBySceneIds(List<Long> sceneIds);
}
